/*
 * Created on March 14, 2011, 9:27 AM
 *
 *  @file:      rnaffe_application_server.cpp
 *  @details    Implementation file for RnaFFEApplicationServer class.\n
 *              System: CombEng \n
 *              Language: C++\n
 *
 *  @author     Favio Bettiol
 *  @email      favio.bettiol AT gmail.com
 *
 *  @author     Diego Diaz
 *  @email      diazdiego86 AT gmail.com
 *
 *  @date       August 2010
 *  @version    0.1
 *
 * This file is part of CombEng
 *
 * CombEng : Combinatory Engine, an FuD's abstraction layer for problems that
 * require to combine elements. Also, this layer provides ranking and prune
 * features for these combinations.
 * <http://fud.googlecode.com/>
 *
 * Copyright (C) 2010 - Favio Bettiol and Diego Diaz
 *
 * CombEng is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CombEng is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with CombEng .  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "../saarna/rnaffe_application_server.h"

const size_t RANK_SIZE = 10;

RnaFFEApplicationServer::RnaFFEApplicationServer(const char* seq, const char* results_file, const IFold* fold)
    : L5ApplicationServer<lav::ResisBasedAntiviral>(), _root_node(NULL), output_file(""), top_ranker(RANK_SIZE),
      bottom_ranker(RANK_SIZE)
{
    // Obtaining the biopp::PseudoNucSequence from the file
    biopp::PseudoNucSequence sequence;
    lav::load_sequence(seq, sequence);

    std::list <size_t> id_list;
    all_antivirals.to_id_list(id_list);

    // Root node creation.
    _root_node = new RnaFFENode(sequence, id_list, fold);
    output_file.open(results_file);
}

RnaFFEApplicationServer::~RnaFFEApplicationServer()
{
    delete _root_node;
    output_file.close();
}

void RnaFFEApplicationServer::get_initial_packet(recabs::Packet& pkt) const
{
    this->_root_node->serialize(pkt);
}

void RnaFFEApplicationServer::receive_result(const recabs::Packet& pkt)
{
    bistream bis(pkt);
    biopp::PseudoNucSequence nuc_seq;
    std::list <float> fe_list;
    std::list <size_t> distances_list;
    Therapy therapy;

    bis >> fe_list >> nuc_seq >> therapy >> distances_list;

    output_file <<  fe_list.size();
    output_file <<"  ";

    size_t acumulated = 0;
    for (std::list<size_t>::const_iterator it = distances_list.begin(); it != distances_list.end(); ++it)
        acumulated += (*it);

    output_file << acumulated << "\n";

    const ScoredTherapy score(distances_list, therapy);
    top_ranker.insert(score);
    bottom_ranker.insert(score);
}

void  RnaFFEApplicationServer::print_top_ranker () const 
{
    for (TopRanking::const_iterator it = top_ranker.begin(); it != top_ranker.end(); ++it)
        it->print();

}

